import { computed, reactive, Ref, ref } from "vue";
import PermissionStore from "@/store/modules/permission";
import { useRoute } from "vue-router";
import { cloneDeep } from "lodash";
import CommonHook from "@/hook/commonHook";
export default function InitHook(state: { mode: Ref<SuperTable.Mode>; column: Ref<Array<SuperTable.TableCoulmnItem | SuperTable.NestTableColumnItem>>; activeKey: Ref<string> }) {
  const { column, activeKey, mode } = state;
  const { isArray } = CommonHook();
  const route = useRoute();
  const configExtend = {
    rowConfig: { isHover: true }
  };
  const xTable = ref<any>(null);
  const drawerOpts = reactive<Base.DrawerOpts>({
    title: "",
    width: "900px",
    visible: false,
    component: "",
    type: 1
  });
  const fieldColumn = ref<Array<SuperTable.TableCoulmnItem | SuperTable.NestTableColumnItem>>([]);
  const init = () => {
    fieldColumn.value = [];
    const config = PermissionStore.fieldColumn[route.path];
    const cloneColumn = cloneDeep(column.value).map((t: any, i: number) => {
      return {
        ...t,
        id: i + 1
      };
    });
    if (config && mode.value === "list") {
      // Tab配置
      if (activeKey.value) {
        if (isArray(config)) {
          console.warn("配置错误,请检查数据类型是否为Object!");
          return (fieldColumn.value = cloneDeep(column.value));
        } // 已存在个性化配置 字段
        const customConfig = config[activeKey.value as any];
        if (!customConfig) {
          return (fieldColumn.value = cloneDeep(column.value).map((t: SuperTable.TableCoulmnItem | SuperTable.NestTableColumnItem) => {
            let fixed;
            if (t.field === "action") {
              fixed = "right";
            } else if (t.field === "checkbox") {
              fixed = "left";
            }
            return {
              ...t,
              fixed: fixed ? fixed : t.fixed,
              isShow: true
            };
          }));
        } // const showField = customConfig.filter((t: any) => t.isShow).map((t: any) => t.field);
        const baseField = cloneColumn.map((t: SuperTable.TableCoulmnItem | SuperTable.NestTableColumnItem) => t.field);
        for (let i = 0; i < customConfig.length; i++) {
          const target = customConfig[i];
          if (!baseField.includes(target.field)) continue;
          if (["checkbox", "action"].includes(target.field)) {
            fieldColumn.value.push({
              ...target,
              fixed: target.field === "action" ? "right" : "left",
              isShow: true
            });
            continue;
          }
          fieldColumn.value.push({
            ...target,
            isShow: target.isShow
          });
          const source = cloneColumn.find((t: SuperTable.TableCoulmnItem | SuperTable.NestTableColumnItem) => t.field === target.field);
          if (source) source.__isOperation__ = true;
        }
        const otherConfig = cloneColumn.filter((t: SuperTable.TableCoulmnItem | SuperTable.NestTableColumnItem) => !t.__isOperation__ && !["checkbox", "action"].includes(t.field));
        for (let i = 0; i < otherConfig.length; i++) {
          fieldColumn.value.push({
            ...otherConfig[i],
            isShow: false
          });
        }
      } else {
        // const showField = config.filter((t: any) => t.isShow).map((t: any) => t.field);
        const baseField = cloneColumn.map((t: SuperTable.TableCoulmnItem | SuperTable.NestTableColumnItem) => t.field);
        for (let i = 0; i < config.length; i++) {
          const target = config[i];
          if (!baseField.includes(target.field)) continue;
          if (["checkbox", "action"].includes(target.field)) {
            fieldColumn.value.push({
              ...target,
              fixed: target.field === "action" ? "right" : "left",
              isShow: target.isShow
            });
            continue;
          }
          fieldColumn.value.push({
            ...target,
            isShow: target.isShow
          });
          const source = cloneColumn.find((t: SuperTable.TableCoulmnItem | SuperTable.NestTableColumnItem) => t.field === target.field);
          if (source) source.__isOperation__ = true;
        }
        const otherConfig = cloneColumn.filter((t: SuperTable.TableCoulmnItem | SuperTable.NestTableColumnItem) => !t.__isOperation__ && !["checkbox", "action"].includes(t.field));
        for (let i = 0; i < otherConfig.length; i++) {
          fieldColumn.value.push({
            ...otherConfig[i],
            isShow: false
          });
        }
      }
    } else {
      // 没有配置 使用默认配置
      fieldColumn.value = cloneDeep(column.value).map((t: SuperTable.TableCoulmnItem | SuperTable.NestTableColumnItem) => {
        let fixed;
        if (t.field === "action") {
          fixed = "right";
        } else if (t.field === "checkbox") {
          fixed = "left";
        }
        return {
          ...t,
          fixed: fixed ? fixed : t.fixed,
          isShow: true
        };
      });
    }
  };
  const showFieldColumn = computed<Array<SuperTable.TableCoulmnItem | SuperTable.NestTableColumnItem>>(() => {
    return fieldColumn.value.filter((t: any) => t.isShow);
  });
  return {
    configExtend,
    xTable,
    drawerOpts,
    init,
    showFieldColumn,
    fieldColumn
  };
}
